package com.itheima.pinda.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.pinda.DTO.user.TruckDriverDto;
import com.itheima.pinda.common.utils.PageResponse;
import com.itheima.pinda.entity.user.PdTruckDriver;
import com.itheima.pinda.mapper.PdTruckDriverMapper;
import com.itheima.pinda.mapstruct.TruckDriverDTO2Entity;
import com.itheima.pinda.service.PdTruckDriverService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class PdTruckDriverServiceImpl extends ServiceImpl<PdTruckDriverMapper, PdTruckDriver> implements PdTruckDriverService {

    /**
     * ServiceImpl<PdTruckDriverMapper, PdTruckDriver>
     * 这个MP的实现类, 已经将PdTruckDriverMapper注入到里面了, 子类PdTruckDriverServiceImpl直接使用
     */

    @Resource
    private TruckDriverDTO2Entity driverDTO2Entity;

    /**
     * 保存司机相关信息
     *
     * @param dto 司机基本信息
     * @return dto
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public TruckDriverDto saveTruckDriver(TruckDriverDto dto) {
        PdTruckDriver pdTruckDriver = driverDTO2Entity.conver(dto);
        PdTruckDriver driver = super.getOne(Wrappers.<PdTruckDriver>lambdaQuery()
                .eq(PdTruckDriver::getUserId, pdTruckDriver.getUserId()));

        // driver 不为空, 就表示更新, 需要将Id设置下
        // driver 为空, 就是表示这是一条新的记录, Id由Mp自动生成策略生成
        if (driver != null) {
            pdTruckDriver.setId(driver.getId());
        }

        super.saveOrUpdate(pdTruckDriver);
        dto = driverDTO2Entity.conver(pdTruckDriver);
        return dto;
    }

    /**
     * 获取司机基本信息列表
     *
     * @param userIds 司机id列表
     * @param fleetId 司机所属车队Id
     * @return 司机基本信息列表
     */
    @Override
    public List<TruckDriverDto> findAllDriver(List<String> userIds, String fleetId) {
        List<PdTruckDriver> pdTruckDriverList = super.list(Wrappers.<PdTruckDriver>lambdaQuery()
                .in(userIds != null && userIds.size() > 0, PdTruckDriver::getUserId, userIds)
                .eq(StringUtils.isNotBlank(fleetId), PdTruckDriver::getFleetId, fleetId)
                .orderByDesc(PdTruckDriver::getId));

        return pdTruckDriverList.stream()
                .map(item -> driverDTO2Entity.conver(item))
                .collect(Collectors.toList());
    }

    /**
     * 获取司机分页数据
     *
     * @param page     页码
     * @param pageSize 页尺寸
     * @param fleetId  车队id
     * @return 司机分页数据
     */
    @Override
    public PageResponse<TruckDriverDto> findByPage(Integer page, Integer pageSize, String fleetId) {
        Page<PdTruckDriver> pdTruckDriverPage = super.page(new Page<>(page, pageSize),
                Wrappers.<PdTruckDriver>lambdaQuery()
                        .eq(StringUtils.isNotBlank(fleetId), PdTruckDriver::getFleetId, fleetId)
                        .orderByDesc(PdTruckDriver::getId));

        return PageResponse.<TruckDriverDto>builder()
                .page(page)
                .pagesize(pageSize)
                .counts(pdTruckDriverPage.getTotal())
                .pages(pdTruckDriverPage.getPages())
                .items(pdTruckDriverPage.getRecords()
                        .stream()
                        .map(item -> driverDTO2Entity.conver(item))
                        .collect(Collectors.toList()))
                .build();
    }
}
